package com.Book.test_1_25_springmvcbook.Service;

import com.Book.test_1_25_springmvcbook.Dao.BookDao;
import com.Book.test_1_25_springmvcbook.enums.BookStatusEnums;
import com.Book.test_1_25_springmvcbook.mapper.BookInfoMapper;
import com.Book.test_1_25_springmvcbook.model.BookInfo;
import com.Book.test_1_25_springmvcbook.model.PageRequest;
import com.Book.test_1_25_springmvcbook.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service // 是数据库和数据之间的一个桥梁
public class BookService {
    //@Autowired// 依赖注入
  //  BookDao bookDao = new BookDao();

    //引入mapper
    @Autowired
    private BookInfoMapper bookInfoMapper;
//
//    public List<BookInfo> getBookList(){
//        List<BookInfo> bookInfos = bookDao.mockData(); //mock表示的是虚拟的假数据
//
//
//        for (BookInfo bookInfo : bookInfos){
//            if(bookInfo.getStatus() == 1){
//                bookInfo.setStatusCN("可借阅");
//            }else{
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest){
        // 校验
        if(pageRequest == null){
            return null;
        }
        // 获取总记录数
        Integer count = bookInfoMapper.count();

        // 获取当前记录数
        // Integer offset = (pageRequest.getCurrentPage() - 1) * pageRequest.getPageSize(); //把这一行放在请求里面


        List<BookInfo> bookInfos = bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());

        if(bookInfos != null && bookInfos.size() >0){
            for (BookInfo bookInfo:bookInfos) {
//                if(bookInfo.getStatus() == 1){
//                    bookInfo.setStatusCN("可借阅");
//                } else if (bookInfo.getStatus() == 2) {
//                    bookInfo.setStatusCN("不可借阅");
//                }
                // 但是这样写不够优雅
                // 定义成一个枚举类来写

                // 根据 status 获取状态的定义
               bookInfo.setStatusCN(BookStatusEnums.getNameByCode(bookInfo.getStatus()).getName());
            }
        }


//        PageResult<BookInfo> pageResult = new PageResult<>();
//        pageResult.setRecords(bookInfos);
//        pageResult.setTotal(count);
        // 但是这三行代码写起来也是比较难看, 我们可以把它封装起来,到 pageResult里面
        return new PageResult<>(bookInfos,count,pageRequest);
    }

    // 添加图书
    public Integer addBook(BookInfo bookInfo){
        Integer result = 0;
        try{
            result = bookInfoMapper.insertBook(bookInfo);

        }catch (Exception e){
            log.error("添加失败, e:{}",e);
        }
        return result;
    }

    // 修改图书
    // 先查询
    public BookInfo queryBookInfoById(Integer id){
        return bookInfoMapper.queryBookInfoById(id);
    }
    // 然后更新
    public Integer updateBook(BookInfo bookInfo){
        Integer result = 0;
        try {
          result =  bookInfoMapper.updateBook(bookInfo);
        }catch (Exception e){
            log.error("修改失败, e:{}",e);
        }
        return result;
    }

    // 批量删除
    public Integer batchDelete(List<Integer> ids){
        Integer result = 0;
        try {
            result = bookInfoMapper.batchDelete(ids);
        }catch (Exception e){
            log.error("批量删除图书失败, ids:{}",e);
        }
        return result;
    }
}
